﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;

namespace Hr.Controllers
{
    public class positionController : Controller
    {
        private readonly ILogger<clientController> _logger;
        private readonly Ifirst_kindServices fdServices;
        private readonly Isecond_kindServices skServices;
        private readonly Ithird_kindServices tkServices;
        private readonly Iconfig_major_kindServices mkServices;
        private readonly Iconfig_majorServices mServices;
        private readonly Iengage_major_releaseServices mrServices;
        private readonly MyDbContext myDbContext;

        public positionController(MyDbContext myDbContext, ILogger<clientController> logger,Ifirst_kindServices fdServices,Isecond_kindServices skServices, Ithird_kindServices tkServices,Iconfig_major_kindServices mkServices,Iconfig_majorServices mServices,Iengage_major_releaseServices mrServices)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.fdServices = fdServices;
            this.skServices = skServices;
            this.tkServices = tkServices;
            this.mkServices = mkServices;
            this.mServices = mServices;
            this.mrServices = mrServices;
        }

        [HttpGet]
        public IActionResult position_register()
        {
            string name = HttpContext.Session.GetString("Name");
            ViewData["name"] = name;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> position_register([FromForm] Eengage_major_release test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDbContext.engage_major_release.Add(test);
                    result = await myDbContext.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());
        }

        [HttpPost]
        public async Task<ActionResult> GetAll()
        {
            List<config_file_first_kind> first = await fdServices.selectAll();
            List<config_file_second_kind> second = await skServices.selectAll();
            List<config_file_third_kind> third = await tkServices.selectAll();
            List<config_major_kind> major_kind = await mkServices.config_major_kindSelect();
            List<config_major> major = await mServices.config_majorSelect();
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["first"] = first;
            di["second"] = second;
            di["third"] = third;
            di["major_kind"] = major_kind;
            di["major"] = major;
            return Content(JsonConvert.SerializeObject(di));
        }

        [HttpPost]
        //三级根据选择一级查询二级
        public async Task<ActionResult> GetfirstId(string id)
        {
            List<config_file_second_kind> second = await skServices.GetFirstId(id);
            return Content(JsonConvert.SerializeObject(second));
        }

        [HttpPost]
        //III级 根据选择II级查询III级
        public async Task<ActionResult> GetsecondId(string id)
        {
            List<config_file_third_kind> third = await tkServices.GetSecondId(id);
            return Content(JsonConvert.SerializeObject(third));
        }

        [HttpPost]
        //职位名称 根据选择职位分类查询职位名称
        public async Task<ActionResult> Getmajor(string id)
        {
            List<config_major> major = await mServices.Getmajor(id);
            return Content(JsonConvert.SerializeObject(major));
        }


        [HttpPost]
        public async Task<IActionResult> position_release_search2([FromQuery] int id)
        {
            Dictionary<string, object> di = new Dictionary<string, object>();
            FenYe fy = new FenYe()
            {
                Where = "",
                CurrentPage = id,
                PageSize = 3
            };
            List<engage_major_release> list = await mrServices.FenYe(fy);
            di["list"] = list;
            di["rows"] = fy.Rows;
            di["pages"] = fy.Pages;
            return Ok(di);
        }

        [HttpGet]
        public IActionResult position_release_search()
        {
            return View();
        }


        [HttpPost]
        public async Task<IActionResult> position_change_update2([FromQuery] int id)
        {
            Dictionary<string, object> di = new Dictionary<string, object>();
            FenYe fy = new FenYe()
            {
                Where = "",
                CurrentPage = id,
                PageSize = 3
            };
            List<engage_major_release> list = await mrServices.FenYe(fy);
            di["list"] = list;
            di["rows"] = fy.Rows;
            di["pages"] = fy.Pages;
            return Ok(di);
        }

        [HttpGet]
        public IActionResult position_change_update()
        {
            return View();
        }


        //查找，修改
        [HttpGet]
        public ActionResult position_release_change(int id)
        {
            ViewData["id"] = id;
            //var fd = await myDbContext.config_file_first_kind.FindAsync(id);
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> position_release_change1(int id)
        {
            var fd = await myDbContext.engage_major_release.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }

        [HttpPut]
        public async Task<IActionResult> position_release_change([FromForm] Eengage_major_release test)
        {
            int result;
            if (ModelState.IsValid)
            {
                myDbContext.engage_major_release.Attach(test);
                myDbContext.Entry(test).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDbContext.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }

        //查询  position_release_details
        [HttpGet]
        public ActionResult position_release_details(int id)
        {
            ViewData["id"] = id;
            //var fd = await myDbContext.config_file_first_kind.FindAsync(id);
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> position_release_details1(int id)
        {
            var fd = await myDbContext.engage_major_release.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }

        //删除
        [HttpDelete]
        public async Task<int> major_release_Delete([FromForm] int id)
        {
            int result = 0;
            try
            {
                Eengage_major_release test = await myDbContext.engage_major_release.FindAsync(id);
                myDbContext.engage_major_release.Remove(test);
                result = await myDbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.Message);
            }
            return result;

        }


    }
}
